package com.wswy.wzcx.ui.dialog;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.app.AppCompatDialog;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.wswy.wzcx.R;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.subscribers.ResourceSubscriber;
import java.util.concurrent.TimeUnit;

public class LongtimeLoadingDialog extends AppCompatDialog {

  private static final String TAG = "LongtimeLoadingDialog";

  private static final String[] TIPS = {"拼命查询中，请等待~","正在连接交管数据","正在解析违章数据"};

  public LongtimeLoadingDialog(Context context) {
    super(context, R.style.customTransparentDialog);
  }

  private ResourceSubscriber<Long> subscriber;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialog_long_time_loading);

    setCanceledOnTouchOutside(false);
    setCancelable(false);

    final TextView tvLoading = findViewById(R.id.tv_loading);

    subscriber = new ResourceSubscriber<Long>() {
      @Override
      public void onNext(Long aLong) {

        if (aLong > 0){
          tvLoading.setText(TIPS[(int)(aLong % TIPS.length)]);
//          Log.e(TAG, "onNext --> "+ );
        }



      }

      @Override
      public void onError(Throwable t) {

      }

      @Override
      public void onComplete() {

      }
    };

    Flowable.interval(0,3,TimeUnit.SECONDS)
        .subscribeOn(AndroidSchedulers.mainThread())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(subscriber);

  }

  @Override
  protected void onStop() {
    super.onStop();
    if (subscriber!= null){
      subscriber.dispose();
    }
  }
}
